package net.chathu.airgw.packets.gw.clientpackets 
{
	import flash.utils.ByteArray;
	import net.chathu.airgw.Config;
	import net.chathu.airgw.model.JoinedRoomInfo;
	import net.chathu.airgw.model.Room;
	import net.chathu.airgw.model.User;
	import net.chathu.airgw.packets.gw.serverpackets.EchoPacket;
	import net.chathu.airgw.packets.gw.serverpackets.EndOfNamesPacket;
	import net.chathu.airgw.packets.gw.serverpackets.ErrorNoSuchChannelPacket;
	import net.chathu.airgw.packets.gw.serverpackets.JoinPacket;
	import net.chathu.airgw.packets.gw.serverpackets.NamesPacket;
	import net.chathu.airgw.packets.gw.serverpackets.TopicPacket;
	import net.chathu.airgw.util.ByteArrayUtil;
	/**
	 * ...
	 * @author ze
	 */
	public class CMDJoin extends IRCReceivablePacket 
	{
		
		public function CMDJoin() 
		{
			
		}
		
		override public function execute():void {
			var targetChannels:Array;
			if (String(data[1]).indexOf(":") == 0)
			{
				targetChannels = String(data[1]).substr(1).split(",");
			} else {
				targetChannels = String(data[1]).split(",");
			}
			
			
			
			for (var i:int = 0; i < targetChannels.length; i++)
			{
				var channelName:String = String(targetChannels[i]);
				if (channelName.indexOf("#") == 0)
				{
					var room:Room = sessionData.chatData.rooms.getRoomByName(channelName);
					if (room)
					{
						if (!sessionData.chatData.isInRoom(room)) {
							//ha nem vagy bennt a szobaban:
							sendToChat(ByteArrayUtil.convertParams(["sub", "rooms/" + room.id + "/users"]));
						} else {
							//ha mar bennt vagy...
						}
					} else {
						sendToClient(new ErrorNoSuchChannelPacket(channelName));
					}
				} else if (channelName.indexOf("&") == 0 && channelName.length >= 2 && channelName.length <= 64) {
					channelName = channelName.toLowerCase();
					if (channelName == Config.USERSLIST_CHANNEL)
					{
						if (sessionData.chatData.userId)
						{
							if (!sessionData.chatData.isInUsersRoom)
							{
								sessionData.chatData.isInUsersRoom = true;
								sendToClient(new JoinPacket(sessionData.yourHost, Config.USERSLIST_CHANNEL));
								sendToClient(new TopicPacket(sessionData.nick, Config.USERSLIST_CHANNEL, "Chat.hu online felhaszálók"));
								var jri:JoinedRoomInfo = new JoinedRoomInfo( -1);
								for (var userId:String in sessionData.chatData.users.getDictionary())
								{
									jri.addUser(parseInt(userId), -1);
								}
								sendToClient(new NamesPacket(sessionData.nick, jri, Config.USERSLIST_CHANNEL, sessionData));
								sendToClient(new EndOfNamesPacket(sessionData.nick, Config.USERSLIST_CHANNEL));
							}
						}
					} else {
						if (!sessionData.isInGroup(channelName))
						{
							sessionData.soul.p2pChatConnection.joinGroup(channelName);
							
							
							////
							//var roomInfo:JoinedRoomInfo = sessionData.chatData.getJoinedRoomInfo(roomId);
							
							//sendToClient(new JoinPacket(user.userHost,room.name));
							//sendToClient(new JoinPacket(sessionData.yourHost,channelName));
							//sendToClient(new TopicPacket(sessionData.nick, room.name, room.topic));
							
							//sendToClient(new NamesPacket(sessionData.nick,roomInfo,room, sessionData));
							//sendToClient(new EndOfNamesPacket(sessionData.nick,room.name));
						}
					}
				} else {
					sendToClient(new EchoPacket(Config.GW_ECHO_NICK, "Can't join to channel: " + channelName));
				}
			}
			
		}
		
		public static function getShortUsageDescription():String {
			return "Belépés szobába";
		}
		
		public static function getUsageDescription():String {
			return "Példa:\n" +
				"/join #eloszoba\n\n" +
				"Eredmény:\n" + 
				"Sikeresen csatlakoztál az Előszoba szobához. A(z) #eloszoba témája ez: Előszoba | Moderátorok: 0";
		}
	}

}